package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyBalanceRecordAddDTO;
import org.jsola.hr.dto.CompanyBalanceRecordUpdateDTO;
import org.jsola.hr.query.CompanyBalanceRecordQuery;
import org.jsola.hr.service.ICompanyBalanceRecordService;
import org.jsola.hr.vo.CompanyBalanceRecordListVO;
import org.jsola.hr.vo.CompanyBalanceRecordVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用工单位账面余额明细表
 *
 * @author zhr
 */
@RestController("hrCompanyBalanceRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyBalanceRecords")
public class CompanyBalanceRecordController implements ICompanyBalanceRecordControllerAPI {

    @Autowired
    private ICompanyBalanceRecordService companyBalanceRecordService;

    @Override
    @PostMapping
    public Result<CompanyBalanceRecordVO> save(@Valid @RequestBody CompanyBalanceRecordAddDTO companyBalanceRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增用工单位账面余额明细表权限校验

        return Result.success(
                companyBalanceRecordService.save(companyBalanceRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyBalanceRecordUpdateDTO companyBalanceRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改用工单位账面余额明细表权限校验

        int count = companyBalanceRecordService.update(companyBalanceRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{companyBalanceRecordId}")
    public Result<Integer> delete(@PathVariable Long companyBalanceRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除用工单位账面余额明细表权限校验

        int count = companyBalanceRecordService.deleteByIds(tokenUser, companyBalanceRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] companyBalanceRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除用工单位账面余额明细表权限校验

        int count = companyBalanceRecordService.deleteByIds(tokenUser, companyBalanceRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyBalanceRecordId}")
    public Result<CompanyBalanceRecordVO> findById(@PathVariable Long companyBalanceRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位账面余额明细表权限校验

        return Result.success(
                companyBalanceRecordService.selectById(companyBalanceRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyBalanceRecordListVO>> listAll(@Valid CompanyBalanceRecordQuery companyBalanceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位账面余额明细表权限校验

        return Result.success(
                companyBalanceRecordService.select(companyBalanceRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyBalanceRecordListVO>> page(@Valid CompanyBalanceRecordQuery companyBalanceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyBalanceRecordService.selectPage(companyBalanceRecordQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("pageByCompanyId")
    public Result<Page<CompanyBalanceRecordListVO>> pageByCompanyId(@Valid @RequestBody CompanyBalanceRecordQuery companyBalanceRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyBalanceRecordService.pageByCompanyId(companyBalanceRecordQuery, tokenUser.getSiteId())
        );
    }

    @PostMapping("init")
    public Result<String> init() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        companyBalanceRecordService.init();
        return Result.success();
    }


}

